/**
 * 
 */
package game.rwalk.control;

import game.rwalk.R;
import game.rwalk.entity.Row;
import game.rwalk.utility.Constants;
import game.rwalk.utility.ProccessFile;

import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.BlurMaskFilter.Blur;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;

/**
 * @author sk8erboi
 *
 */
public class Carousel {
	
	private ArrayList<Row> rows = new ArrayList<Row>();
	private Paint paint = new Paint();
	private int hitColor;
	
	private ProccessFile procFile;
	private int[][] array ;
	
	public boolean hit = false;

	public ArrayList<Row> getRows() {
		return rows;
	}

	public void setRows(ArrayList<Row> rows) {
		this.rows = rows;
	}

	private int speed;
	
	//create a rectangle for cover carousel scope
	private Rect scope;

	//contructor
	public Carousel(int[] ids, int[] index, Context mContex) {
		
		procFile = new ProccessFile(mContex.getResources().openRawResource(R.drawable.color2));
		array = procFile.getIndex();
		
//		for(int i = 0; i < 5; i++) {
//			for(int j = 0; j < 3; j++) {
//				System.out.println(array[i][j]);
//			}
//		}
		
		//define speed in normal mode
		speed = Constants.BASE_SPEED;
		
		scope = new Rect(0,
						 Constants.STATUS_GAME_WIDTH + Constants.BLUR_GAME_HEIGHT,
						 Constants.DISPLAY_WIDTH,
						 Constants.DISPLAY_HEIGHT
						);
		
		paint.setMaskFilter(new BlurMaskFilter(5, BlurMaskFilter.Blur.NORMAL));
		
		//add 5 row into the carousel
		for(int i = 0; i < Constants.NUMBER_ROW; i++) {
			if(i == 5) {
				rows.add(new Row(ids, array[0], mContex));
			} else {
				rows.add(new Row(ids, array[i], mContex));
			}
		}
	}
	
	public void init() {
		for(int i = 0; i < Constants.NUMBER_ROW; i++) {
			rows.get(i).setyRow(Constants.STATUS_GAME_WIDTH + Constants.BLUR_GAME_HEIGHT + i*Constants.HEIGHT_CELL);
		}
	}
	
	public boolean onDraw(Canvas canvas, Paint bitmapPaint) {
		
		//draw row
		for(int i = 0; i < Constants.NUMBER_ROW; i++) {
			rows.get(i).onDraw(canvas,
								rows.get(i).getyRow(),
								bitmapPaint
							   );
		}
		
		//test field focus of game
		/*Paint paint = new Paint();
		paint.setAlpha(0);
		paint.setARGB(200, 20, 100, 10);
		canvas.drawRect(scope, paint);*/
		
		//draw blur field
		canvas.drawRect(	0, Constants.STATUS_GAME_WIDTH - 20,
							Constants.DISPLAY_WIDTH,
							200,
							paint);
		
		canvas.drawRect(	0, Constants.DISPLAY_HEIGHT - 20,
							Constants.DISPLAY_WIDTH,
							Constants.DISPLAY_HEIGHT + 20,
							paint);
		
		return true;
	}
	
	public void checkOutScope() {
		for(int i = 0; i < Constants.NUMBER_ROW; i++) {
			
			//get bottom of a row
			int bottomRow = rows.get(i).getyRow() + Constants.HEIGHT_CELL;
			
			if(bottomRow <= scope.top) {
				
				rows.get(i).setyRow(scope.bottom - speed);
				
				//reset row
				rows.get(i).reset();
				
			} else {
				
				rows.get(i).setyRow(rows.get(i).getyRow() - speed);
			}
		}
	}
	
	public void hitRow(int x, int y) {
		
		//check hit focus field
		if(x > Constants.MARGIN_WIDTH && x < (Constants.MARGIN_WIDTH + 3*Constants.WIDTH_CELL)) {
			if(y > (Constants.STATUS_GAME_WIDTH + Constants.BLUR_GAME_HEIGHT + 20) && y < (Constants.DISPLAY_HEIGHT - 20)) {
				hit = true;
			} else {
				hit = false;
			}
		} else {
			hit = false;
		}
		
		for(int i = 0; i < Constants.NUMBER_ROW; i++) {
			
			int topRect = rows.get(i).getyRow();
			int bottomRect = rows.get(i).getyRow() + Constants.HEIGHT_CELL;
			if(y >= scope.top + 30 && y< scope.bottom - 30) {
				if(y >= topRect && y <= bottomRect && rows.get(i).isActive) {
					
					rows.get(i).isActive = false;
					rows.get(i).hitCell(x);
					hitColor = rows.get(i).getHitColor();
				}
			} else {
				
			}
		}
	}
	
	public int getHitColor() {
		return hitColor;
	}
}
